package com.diandian.backend.entrance.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.diandian.backend.entrance.client.DianDianMemberClient;
import com.diandian.backend.entrance.client.dto.DouyinLifeMemberInfoAddParamDTO;
import com.diandian.backend.entrance.client.dto.DouyinLifeMemberInfoDeleteParamDTO;
import com.diandian.backend.entrance.client.dto.MemberDouyinLifeMemberInfoPageParamDTO;
import com.diandian.backend.entrance.constants.EntranceConstant;
import com.diandian.backend.entrance.dto.ClientPermissionCacheDataDTO;
import com.diandian.base.boot.starter.exception.DefaultException;
import com.diandian.base.boot.starter.response.BaseResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class RouterMemberService {

    private static final Logger logger = LoggerFactory.getLogger(RouterMemberService.class);

    @Autowired
    private DianDianMemberClient memberClient;

    /**
     * 子路由服务
     * @param clientKey
     * @param clientSecret
     * @param data
     * @return
     */
    public Object execute(ClientPermissionCacheDataDTO clientInfoDTO, String clientKey, String clientSecret, String cmd, JSONObject data) {

        // 分页查询会员
        if(EntranceConstant.ROUTER_MEMBER_QUERY_PAGE.equals(cmd)){

            logger.info("执行分页查询会员请求，参数: {}", data == null ? "" : data.toJSONString());

            String belong = clientInfoDTO.getSettingValueFromSetting(EntranceConstant.SETTING_TYPE_CLIENT, EntranceConstant.SETTING_TYPE_CLIENT_BELONG);
            if(belong == null || "".equals(belong)){
                throw new DefaultException("配置缺失");
            }

            // 参数转换
            MemberDouyinLifeMemberInfoPageParamDTO paramDTO = JSONObject.parseObject(data.toString(), MemberDouyinLifeMemberInfoPageParamDTO.class);

            // 执行短信发送
            BaseResult resultDTO = memberClient.page(belong, paramDTO);

            logger.info("分页查询会员请求结果: {}", JSON.toJSONString(resultDTO));

            // 返回结果
            return resultDTO;
        }

        // 同步会员
        if(EntranceConstant.ROUTER_MEMBER_SYCH.equals(cmd)){

            logger.info("执行同步会员请求，参数: {}", data == null ? "" : data.toJSONString());

            String belong = clientInfoDTO.getSettingValueFromSetting(EntranceConstant.SETTING_TYPE_CLIENT, EntranceConstant.SETTING_TYPE_CLIENT_BELONG);
            if(belong == null || "".equals(belong)){
                throw new DefaultException("配置缺失");
            }

            // 参数转换
            DouyinLifeMemberInfoAddParamDTO paramDTO = JSONObject.parseObject(data.toString(), DouyinLifeMemberInfoAddParamDTO.class);

            // 执行短信发送
            BaseResult resultDTO = memberClient.sych(belong, paramDTO);

            logger.info("同步会员请求结果: {}", JSON.toJSONString(resultDTO));

            // 返回结果
            return resultDTO;
        }

        // 移除会员
        if(EntranceConstant.ROUTER_MEMBER_REMOVE.equals(cmd)){

            logger.info("执行移除会员请求，参数: {}", data == null ? "" : data.toJSONString());

            String belong = clientInfoDTO.getSettingValueFromSetting(EntranceConstant.SETTING_TYPE_CLIENT, EntranceConstant.SETTING_TYPE_CLIENT_BELONG);
            if(belong == null || "".equals(belong)){
                throw new DefaultException("配置缺失");
            }

            // 参数转换
            DouyinLifeMemberInfoDeleteParamDTO paramDTO = JSONObject.parseObject(data.toString(), DouyinLifeMemberInfoDeleteParamDTO.class);

            // 执行短信发送
            BaseResult resultDTO = memberClient.remove(belong, paramDTO);

            logger.info("移除会员请求结果: {}", JSON.toJSONString(resultDTO));

            // 返回结果
            return resultDTO;
        }

        throw new DefaultException("暂不支持此操作");
    }

}
